package com.jeecg.scores.service.impl;
import com.jeecg.scores.service.ScoresGrossServiceI;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;

import com.jeecg.scores.entity.BranchWeightEntity;
import com.jeecg.scores.entity.ScoresGrossEntity;
import com.jeecg.scores.page.ScoresGrossPage;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.core.util.oConvertUtils;
import org.jeecgframework.core.util.ApplicationContextUtil;
import org.jeecgframework.core.util.MyClassLoader;
import org.jeecgframework.web.cgform.enhance.CgformEnhanceJavaInter;
import java.util.ArrayList;
import java.util.Date;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Map;
import java.util.HashMap;
import org.jeecgframework.minidao.util.FreemarkerParseFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.jeecgframework.core.util.ResourceUtil;

@Service("scoresGrossService")
@Transactional
public class ScoresGrossServiceImpl extends CommonServiceImpl implements ScoresGrossServiceI {

	@Autowired
	private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
	
 	public void delete(ScoresGrossEntity entity) throws Exception{
 		super.delete(entity);
 	}
	
	public void addMain(ScoresGrossPage scoresGrossPage,HttpServletRequest request) throws Exception {
		ScoresGrossEntity scoresGross = new ScoresGrossEntity();
		MyBeanUtils.copyBeanNotNull2Bean(scoresGrossPage, scoresGross);
		//保存主信息
		String yearsMonthsStr = scoresGross.getYearsMonthsStr();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
		Date date = new Date();
		try {
			date = simpleDateFormat.parse(yearsMonthsStr);
		} catch (Exception e) {
			throw new BusinessException("您输入的日期格式不合法！合法格式例如为：2018-09");
		}
		scoresGross.setYearsMonths(date);
		this.save(scoresGross);
		/**保存-分数分配*/
		List<BranchWeightEntity> branchWeightList = scoresGrossPage.getBranchWeightList();
		for (int i = 0; i < branchWeightList.size(); i++) {
			BranchWeightEntity branchWeight = branchWeightList.get(i);
			//外键设置
			branchWeight.setFkId(scoresGross.getId());
			String branchName = request.getParameter("branchWeightListBranchNameX"+i+"X");
			if (StringUtils.isNotBlank(branchName)) {
				branchWeight.setBranchName(branchName);
			}
			String branchCode = request.getParameter("branchWeightListBranchCodeX"+i+"X");
			if (StringUtils.isNotBlank(branchCode)) {
				branchWeight.setBranchCode(branchCode);
			}
			String principalName = request.getParameter("branchWeightListPrincipalNameX"+i+"X");
			if (StringUtils.isNotBlank(principalName)) {
				branchWeight.setPrincipalName(principalName);
			}
			String principalCode = request.getParameter("branchWeightListPrincipalCodeX"+i+"X");
			if (StringUtils.isNotBlank(principalCode)) {
				branchWeight.setPrincipalCode(principalCode);
			}
			this.save(branchWeight);
		}
	}

	
	public void updateMain(ScoresGrossPage scoresGrossPage) throws Exception{
		ScoresGrossEntity scoresGross = new ScoresGrossEntity();
		//保存主表信息
		if(StringUtil.isNotEmpty(scoresGrossPage.getId())){
			scoresGross = findUniqueByProperty(ScoresGrossEntity.class, "id", scoresGrossPage.getId());
			MyBeanUtils.copyBeanNotNull2Bean(scoresGrossPage, scoresGross);
			this.saveOrUpdate(scoresGross);
		}else{
			this.saveOrUpdate(scoresGross);
		}
		//===================================================================================
		//获取参数
		Object id0 = scoresGross.getId();
		//===================================================================================
		//1.分数分配数据库的数据
	    String hql0 = "from BranchWeightEntity where 1 = 1 AND fkId = ? ";
	    List<BranchWeightEntity> branchWeightOldList = this.findHql(hql0,id0);
		//2.分数分配新的数据
		List<BranchWeightEntity> branchWeightList = scoresGrossPage.getBranchWeightList();
	    //3.筛选更新明细数据-分数分配
		if(branchWeightList!=null &&branchWeightList.size()>0){
			for(BranchWeightEntity oldE:branchWeightOldList){
				boolean isUpdate = false;
				for(BranchWeightEntity sendE:branchWeightList){
					//需要更新的明细数据-分数分配
					if(oldE.getId().equals(sendE.getId())){
		    			try {
							MyBeanUtils.copyBeanNotNull2Bean(sendE,oldE);
							this.saveOrUpdate(oldE);
						} catch (Exception e) {
							e.printStackTrace();
							throw new BusinessException(e.getMessage());
						}
						isUpdate= true;
		    			break;
		    		}
		    	}
	    		if(!isUpdate){
		    		//如果数据库存在的明细，前台没有传递过来则是删除-分数分配
		    		super.delete(oldE);
	    		}
	    		
			}
			//4.持久化新增的数据-分数分配
			for(BranchWeightEntity branchWeight:branchWeightList){
				if(oConvertUtils.isEmpty(branchWeight.getId())){
					//外键设置
					branchWeight.setFkId(scoresGross.getId());
					this.save(branchWeight);
				}
			}
		}
	}

	public void delMain(ScoresGrossEntity scoresGross) throws Exception{
		//删除主表信息
		this.delete(scoresGross);
		//===================================================================================
		//获取参数
		Object id0 = scoresGross.getId();
		//===================================================================================
		//删除-分数分配
	    String hql0 = "from BranchWeightEntity where 1 = 1 AND fkId = ? ";
	    List<BranchWeightEntity> branchWeightOldList = this.findHql(hql0,id0);
		this.deleteAllEntitie(branchWeightOldList);
	}
 	
}